package cqrtplm.controller;

import cn.hutool.core.util.ObjectUtil;
import com.hustcad.plm.pdm.file.handler.response.ResponseResult;
import cqrtplm.constant.RTErrorCodeEnum;
import cqrtplm.constant.RouteConstant;
import cqrtplm.dto.GenerateDescriptionRuleDto;
import cqrtplm.service.CodeSequenceService;
import cqrtplm.service.InquiryMaterialNumberService;
import cqrtplm.vo.DescriptionVO;
import cqrtplm.vo.DrawingCodePartVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(RouteConstant.CODE + "v1")
@Api(tags = "编码")
public class CodeSequenceController {

    @Resource
    private CodeSequenceService codeSequenceService;
    @Resource
    private InquiryMaterialNumberService inquiryMaterialNumberService;

    @GetMapping("/drawingCodeUpgraded")
    @ApiOperation(value = "drawingCodeUpgraded",notes = "图样代号升级")
    public ResponseResult drawingCodeUpgraded(String drawingCode) {
        if(StringUtils.isNotBlank(drawingCode)&&StringUtils.isNotEmpty(drawingCode)){
            codeSequenceService.drawingCodeUpgraded(drawingCode);
            return ResponseResult.success("图样代号升级成功");
        }
        return ResponseResult.fail(RTErrorCodeEnum.DRAWING_CODE_NAME_ERROR.getException("图样代号不能为空"), "图样代号不能为空");
    }

    @RequestMapping("/generateDescriptionRule")
    @ApiOperation(value = "generateDescriptionRule",notes = "生成描述规则")
    public ResponseResult<DescriptionVO> generateDescriptionRule(@RequestBody GenerateDescriptionRuleDto dto) {
        DescriptionVO descriptionVO = codeSequenceService.generateDescriptionRule(dto, Boolean.TRUE);
        if (ObjectUtil.isNotEmpty(descriptionVO)) {
            return ResponseResult.success(descriptionVO,"描述规则生成成功") ;
        } else {
            return ResponseResult.fail(RTErrorCodeEnum.DESCRIBE_RULE_ERROR.getException("描述规则不存在"), "描述规则不存在");
        }
    }

    @RequestMapping("/generateProcessCode")
    @ApiOperation(value = "generateProcessCode",notes = "生成工艺编码")
    public String generateProcessCode() {
        return codeSequenceService.generateProcessCode();
    }

    @RequestMapping("/getMaterialCodeList")
    @ApiOperation(value = "getMaterialCodeList",notes = "图样号")
    public List<DrawingCodePartVO> getMaterialCodeList(String type) {
        return inquiryMaterialNumberService.queryDrawingCodeList(type);
    }

    @RequestMapping("/getIbaName")
    public List<String> getIbaName(String type) {
        return inquiryMaterialNumberService.queryIbaName(type);
    }
}